/**
 * 
 */
package jvs.peer.vfs;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.util.Comparator;

import x.java.io.File.Collector;
import x.java.io.File.Cursor;
import x.java.io.File.Index;

import jvs.vfs.IFile;
import jvs.vfs.IFileBaseImpl;

/**
 * @author qiangli
 * 
 */
public class BuddyFileImpl extends IFileBaseImpl {
	private IFileBaseImpl impl;

	private String group;

	private String peer;

	private String path;

	/**
	 * @param uri
	 */
	public BuddyFileImpl(URI uri) {
		super(uri);

		// buddy:/group/peer/path
		if (uri.getScheme() == null
				|| !uri.getScheme().equalsIgnoreCase("buddy")) {
			throw new IllegalArgumentException("incorrect scheme: "
					+ uri.getScheme());
		}
		String us = uri.toString();
		us = us.substring(6).trim();
		int len = us.length();
		if (len == 0 || us.charAt(0) != '/') {
			throw new IllegalArgumentException("path not absolute: " + us);
		}
		// path = /
		if (len == 1) {
			impl = new GroupFileImpl(group, peer);
			return;
		}
		String[] pa = us.substring(1).split("/", 3);
		group = pa[0];
		peer = pa.length > 1 ? pa[1] : null;
		path = pa.length > 2 ? pa[2] : null;
		if (peer == null) {
			impl = new GroupFileImpl(group, peer);
			return;
		} 
		impl = new PeerFileImpl(peer, path);
	}

	public boolean canRead() {
		return impl.canRead();
	}

	public boolean canWrite() {
		return impl.canWrite();
	}

	public boolean copy(URI uri) {
		return impl.copy(uri);
	}

	public boolean create() {
		return impl.create();
	}

	public boolean delete() {
		return impl.delete();
	}

	public void deleteOnExit() {
		impl.deleteOnExit();
	}

	public boolean exists() {
		return impl.exists();
	}

	public Object getAttribute(String name) {
		return impl.getAttribute(name);
	}

	public String getContent() {
		return impl.getContent();
	}

	public String getContentEncoding() {
		return impl.getContentEncoding();
	}

	public String getContentType() {
		return impl.getContentType();
	}

	public InputStream getInputStream() {
		return impl.getInputStream();
	}

	public long getLastModified() {
		return impl.getLastModified();
	}

	public long getLength() {
		return impl.getLength();
	}

	public OutputStream getOutputStream() {
		return impl.getOutputStream();
	}

	public OutputStream getOutputStream(boolean append) {
		return impl.getOutputStream(append);
	}

	public boolean isDirectory() {
		//top
		if (group == null && peer == null) {
			return true;
		}
		return impl.isDirectory();
	}

	public boolean isEmpty() {
		return impl.isEmpty();
	}

	public boolean isFile() {
		return impl.isFile();
	}

	public boolean isLocal() {
		// treat group/peer as local
		return (path == null);
	}

	public boolean isNative() {
		return false;
	}

	public String[] list() {
		return impl.list();
	}

	public String[] list(int off, int max) {
		return impl.list(off, max);
	}

	public Cursor listc(FileFilter filter, Comparator<File> c) {
		return impl.listc(filter, c);
	}

	public IFile[] listFiles() {
		return impl.listFiles();
	}

	public IFile[] listFiles(FileFilter filter) {
		return impl.listFiles(filter);
	}

	public IFile[] listFiles(FilenameFilter filter) {
		return impl.listFiles(filter);
	}

	public IFile[] listFiles(int off, int max) {
		return impl.listFiles(off, max);
	}

	public int listi(int pagesize, Collector<Index> indice) {
		return impl.listi(pagesize, indice);
	}

	public boolean mkdir() {
		return impl.mkdir();
	}

	public boolean move(URI uri) {
		return impl.move(uri);
	}

	public void refresh() {
		impl.refresh();
	}

	public boolean setAttribute(String name, Object value) {
		return impl.setAttribute(name, value);
	}

	public boolean setContent(String s) {
		return impl.setContent(s);
	}

	public boolean setContentEncoding(String encoding) {
		return impl.setContentEncoding(encoding);
	}

	public boolean setContentType(String mimeType) {
		return impl.setContentType(mimeType);
	}

	public boolean setInputStream(InputStream in) {
		return impl.setInputStream(in);
	}

	public boolean setLastModified(long time) {
		return impl.setLastModified(time);
	}

	public boolean setLength(long len) {
		return impl.setLength(len);
	}

	public boolean setOutputStream(OutputStream out) {
		return impl.setOutputStream(out);
	}
}
